package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.domain.AppRouseAd;
import com.lebang.model.Whether;
import com.lebang.service.AppRouseAdService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;


/**
 * 数据源排序
 *
 * @author hwq
 * @date 2020-03-16
 */
@Controller
@RequestMapping("/sys/approusead")
public class AppRouseAdController {
    @Autowired
    private AppRouseAdService baseService;

    @GetMapping("/list")
    public String list(Model model, String sdkKey) {
        model.addAttribute("sdkKey", sdkKey);
        return "/approusead/list";
    }

    @GetMapping("/add")
    public String add(Integer id, Model model, String sdkKey) {
        AppRouseAd vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppRouseAd();
            vo.setSdkKey(sdkKey);
        }
        model.addAttribute("item", vo);
        return "/approusead/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(AppRouseAd vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            vo.setEnable(Whether.YES.getDbIndex());
            baseService.insertModel(vo);
        }
        baseService.initByKey();
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        AppRouseAd vo = baseService.selectById(id);
        baseService.initByKey();
        baseService.deleteById(id);
        return Callback.success("success", "删除成功");
    }

    @Login
    @PostMapping("/enable")
    @ResponseBody
    public JsonData enable(Integer id, Integer status) {
        AppRouseAd vo = baseService.selectById(id);
        vo.setEnable(status);
        vo.setModifyTime(LocalDateTime.now());
        baseService.updateById(vo);
        baseService.initByKey();
        return Callback.success("success", "操作成功");
    }
}
